using System;

namespace ICSharpCode.TextEditor.Formatting.Intellisense
{
    public class JqueryFunctionCompletionData : FunctionCompletionData
    {
        public JqueryFunctionCompletionData(string text, int imageIndex)
            : base(text, imageIndex)
        {
        }

        public JqueryFunctionCompletionData(string text, string description, int imageIndex)
            : base(text, description, imageIndex)
        {
        }

        private string GetIndentString(int n)
        {
            string text = string.Empty;
            for (int i = 0; i < n; i++)
            {
                text += " ";
            }
            return text;
        }

        public override bool InsertAction(TextArea textArea, char ch)
        {
            string wordBeforeCaret = textArea.GetWordBeforeCaret();
            Caret caret = textArea.Caret;
            int num = caret.Offset + 1;
            int num2 = num;
            if (!string.IsNullOrEmpty(wordBeforeCaret))
            {
                num -= wordBeforeCaret.Length + 1;
                num2--;
                if (num2 - num > 0)
                {
                    textArea.Document.Remove(num, num2 - num);
                }
                textArea.Caret.Position = textArea.Document.OffsetToPosition(Math.Min(num, textArea.Document.TextLength));
            }
            int num3 = caret.Column;
            string text = base.text + "(";
            switch (wordBeforeCaret)
            {
                case "$.":
                    num3 += 2;
                    break;
                case ").":
                    num3 += 2;
                    break;
                case ".":
                    num3 = -1;
                    break;
            }
            switch (base.text)
            {
                case "ajax":
                    text += "{";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "url:url,";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "data:null,";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "type:\"post\",";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "dataType:\"json\",";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "success: function (data) {";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "},";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "error:function (XMLHttpRequest, textStatus, errorThrown) { ";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "}";
                    text = text + Environment.NewLine + GetIndentString(num3 - 2) + "}";
                    break;
                case "post":
                    text += "\"url\",{";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "},";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "function (data) {";
                    text = text + Environment.NewLine + GetIndentString(num3 - 2) + "}";
                    break;
                case "get":
                    text += "\"url\",{";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "},";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "function (data) {";
                    text = text + Environment.NewLine + GetIndentString(num3 - 2) + "}";
                    break;
                case "getJSON":
                    text += "\"url\",function (data) {";
                    text = text + Environment.NewLine + GetIndentString(num3 - 2) + "}";
                    break;
                case "each":
                    text += "arrayObject, function (index, item){";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "// demo";
                    text = text + Environment.NewLine + GetIndentString(num3 - 2) + "}";
                    break;
                case "ready":
                    text += "function (){";
                    text = text + Environment.NewLine + GetIndentString(num3 + 2) + "//do something...";
                    text = text + Environment.NewLine + GetIndentString(num3 - 12) + "}";
                    break;
            }
            text += ")";
            switch (wordBeforeCaret)
            {
                case "$.":
                    text = "$." + text;
                    break;
                case "\").":
                    text = "\")." + text;
                    break;
                case "').":
                    text = "')." + text;
                    break;
                case ").":
                    text = ")." + text;
                    break;
                case ".":
                    text = "." + text;
                    break;
            }
            textArea.InsertString(text);
            return false;
        }
    }
}
